﻿using System;
using Marbles.Core.Connectors;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Marbles.Web.Admin.Downloads
{
    /// <summary>
    /// Summary description for UserProfile
    /// </summary>
    public class UserProfile : DownloadHandler
    {
        public override void Download(HttpResponse response)
        {
            Command cmd = new Command("SELECT a.userid, a.username, a.fullname, a.email, a.lastlogin, a.openid, a.enabled FROM Users a WHERE siteid = @siteid ORDER BY a.username ASC");
            Command cmd1 = new Command("SELECT questionid, shortdescription FROM profiles WHERE siteid = @siteid ORDER BY questionid ASC");
            string siteid = this.CurrentWeb.SiteId;
            DataConnection dbc = new DataConnection();
            cmd.AddParameter("@siteid", siteid);
            cmd1.AddParameter("@siteid", siteid);
            DataTable dtProf = dbc.FetchDataTable(cmd1);

            response.Write("\"Username\",\"Fullname\",\"Email\",\"Last Login\",\"OpenID\",\"Enabled\"");

            for (int i = 0; i < dtProf.Rows.Count; i++)
            {                
                response.Write(string.Format(",\"{0}\"", dtProf.Rows[i]["shortdescription"]));
            }
            response.Write("\n");

            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                response.Write(string.Format("\"{0}\",", dr["username"]));
                response.Write(string.Format("\"{0}\",", dr["fullname"]));
                response.Write(string.Format("\"{0}\",", dr["email"]));
                response.Write(string.Format("\"{0}\",", dr["lastlogin"]));
                response.Write(string.Format("\"{0}\",", dr["openid"]));
                response.Write(string.Format("\"{0}\"", (Convert.ToBoolean(dr["enabled"]) ? "YES" : "NO")));

                if (dtProf.Rows.Count > 0)
                {
                    cmd1 = new Command("SELECT questionid, response FROM userprofiles a WHERE siteid = @siteid AND userid = @userid ORDER BY questionid ASC");
                    cmd1.AddParameter("@siteid", siteid);
                    cmd1.AddParameter("@userid", dr["userid"].ToString());
                    DataTable dt = dbc.FetchDataTable(cmd1);
                    if (dt.Rows.Count > 0)
                    {
                        DataView dv = new DataView(dt);

                        for (int i = 0; i < dtProf.Rows.Count; i++)
                        {
                            dv.RowFilter = "questionid=" + dtProf.Rows[i]["questionid"].ToString();
                            if (dv.Count > 0)
                            {
                                response.Write(",\"" + dv[0]["response"].ToString() + "\"");
                            }
                            else
                            {
                                response.Write("\"\"");
                            }
                        }
                    }
                }

                response.Write("\n");
            }
        }
    }
}